import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/modle/pingtuan/bloc/pingtuan_running_bloc.dart';
import 'package:shopping_group/modle/pingtuan/entity/groupIns4app_record_entity.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/route_util.dart';
import 'package:shopping_group/utils/widget_utils.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';

import 'pingtuan_details_page.dart';

class PingtuanCompletePage extends StatefulWidget {
  PingtuanCompletePage({Key key}) : super(key: key);

  @override
  _PingtuanCompletePageState createState() => _PingtuanCompletePageState();
}

class _PingtuanCompletePageState extends State<PingtuanCompletePage> {
  List<GroupIns4AppRecordEntity> _listEntity = [];
  //listview的控制器
  ScrollController _scrollController = ScrollController();
  PingTuanRunningBloc _bloc;
  @override
  void initState() {
    super.initState();
    _bloc = PingTuanRunningBloc();
    _bloc.initDate.add(BaseConstant.PT_SUC);
    _initListener();
  }

  _initListener() {
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        _bloc.moreDate.add(_scrollController);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, '券已提交'),
      body: Container(
        height: double.infinity,
        decoration: BoxDecoration(
          color: Colours.gray_f0,
        ),
        child: _showlistView(),
      ),
    );
  }

  Widget _showlistView() {
    return StreamBuilder(
      stream: _bloc.sendData,
      initialData: _listEntity,
      builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
        Object object = snapshot.data;
        if (object is List) {
          _listEntity = object;
        }
        int count = _listEntity.length;
        if (0 != count) {
          return Container(
            decoration: new BoxDecoration(
              color: Colors.white,
            ),
            child: AnimationLimiter(
              child: RefreshIndicator(
                onRefresh: _pullToRefresh,
                child: ListView.builder(
                  controller: _scrollController,
                  shrinkWrap: true,
                  padding: const EdgeInsets.only(top: 4),
                  itemCount: _listEntity.length,
                  itemBuilder: buildItem,
                ),
              ),
            ),
          );
        } else {
          return Container(
            alignment: Alignment.center,
            child: WidgetUtils.emptyView(
              contexts,
            ),
          );
        }
      },
    );
  }

  //下拉刷新,必须异步async不然会报错
  Future _pullToRefresh() async {
    _listEntity.clear();
    _bloc.initDate.add(BaseConstant.PT_SUC);
    return null;
  }

  //ListView的Item
  Widget buildItem(BuildContext context, int index) {
    return Container(
      // margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: _xunchaItem(_listEntity[index]),
    );
  }

  Widget _xunchaItem(GroupIns4AppRecordEntity entity) {
    return Container(
        padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 8),
        child: Column(
          children: <Widget>[
            SizedBox(
              height: 4,
            ),
            Padding(
              padding: const EdgeInsets.only(left: 4),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  WidgetUtils.getTextColorView(
                    '恭喜推广成功',
                    Colours.blackground_609,
                    Dimens.FONT_SIZE_16,
                  ),
                  WidgetUtils.getTextColorView(
                    '代理券提交成功',
                    Colours.themeColor,
                    Dimens.FONT_SIZE_16,
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 4,
            ),
            Row(
              children: <Widget>[
                Container(
                  height: 110.0,
                  width: 110.0,
                  margin: const EdgeInsets.all(5.0),
                  child: FadeInImage.assetNetwork(
                    image: entity.imgUrl,
                    placeholder: 'images/picture_loading.png' /* 指定gif */,
                    fit: BoxFit.contain,
                  ),
                ),
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.only(left: 16),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        SizedBox(
                          height: 4,
                        ),
                        WidgetUtils.getTextColorView(
                          entity.name,
                          Colours.text_2222,
                          Dimens.FONT_SIZE_14,
                        ),
                        SizedBox(
                          height: 4,
                        ),
                        // Column(
                        //   mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        //   crossAxisAlignment: CrossAxisAlignment.start,
                        //   children: <Widget>[
                        //     WidgetUtils.getTextColorView('先付款,后选品；购物不纠结,让推广更简单',
                        //         Colours.gray_91, Dimens.FONT_SIZE_14),
                        //     SizedBox(
                        //       height: 4,
                        //     ),
                        //     WidgetUtils.getTextColorView(
                        //         '¥${entity.rechargeVal}/件 共1件商品',
                        //         Colours.text_2222,
                        //         Dimens.FONT_SIZE_14,
                        //         decorationColor: Colours.text_2222),
                        //   ],
                        // ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
            SizedBox(
              height: 4,
            ),
            Padding(
              padding: const EdgeInsets.only(left: 4),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  WidgetUtils.getTextColorView(
                    '推广时间:${entity.joinTime}',
                    Colours.text_2222,
                    Dimens.FONT_SIZE_14,
                  ),
                  // WidgetUtils.getTextColorView(
                  //   '实付款：¥${entity.rechargeVal}',
                  //   Colours.text_2222,
                  //   Dimens.FONT_SIZE_14,
                  // ),
                ],
              ),
            ),
            // SizedBox(
            //   height: 8,
            // ),
            // Container(
            //   alignment: Alignment.centerRight,
            //   child: Row(
            //     crossAxisAlignment: CrossAxisAlignment.end,
            //     mainAxisAlignment: MainAxisAlignment.end,
            //     children: <Widget>[
            //       GestureDetector(
            //         onTap: () {
            //           RouteUtil.goParameter(PingtuanDetailsPage(
            //             entity: entity,
            //           ));
            //         },
            //         child: WidgetUtils.showBoxBotton('订单详情',
            //             borderColor: Colours.gray_91,
            //             textColor: Colours.gray_91,
            //             borderRadius: 20),
            //       ),
            //     ],
            //   ),
            // ),
          ],
        ));
  }

  @override
  void dispose() {
    super.dispose();
  }
}
